﻿using CPPEI.Coolzon.Core.Data;
using CPPEI.Coolzon.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Core.Service
{
    /// <summary>
    /// 版本服务
    /// </summary>
    public class VersionService
    {
        /// <summary>
        /// 版本签入
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="fileFullName"></param>
        /// <param name="changeset"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public VersionResult Checkin(string userName,string fileFullName,double changeset,byte[] data)
        {
            //首先判断是否存在，存在，执行更新，不存在，添加。
            VersionResult versionResult = new VersionResult();
            try
            {
                using (DbContext content = new DbContext())
                {
                     IVersionFileDataRepository versionFileDataRepository = RepositoryFactory.Create<IVersionFileDataRepository>();

                     VersionFileData versionFileData = new VersionFileData();
                     versionFileData.Id = Guid.NewGuid();
                     versionFileData.Data = data;

                    IVersionFileChangesetRepository versionFileChangesetRepository = RepositoryFactory.Create<IVersionFileChangesetRepository>();

                    VersionFileChangeset versionFileChangeset = new VersionFileChangeset();
                    versionFileChangeset.Id =Guid.NewGuid();
                    versionFileChangeset.ChangedDate = DateTime.Now;
                    versionFileChangeset.ChangedUser = userName;
                    versionFileChangeset.Changeset =changeset;
                    //说明暂时未空
                    versionFileChangeset.Comment="";
                    versionFileChangeset.FileDataId =versionFileData.Id;

                    IVersionFileRepository versionFileRepository = RepositoryFactory.Create<IVersionFileRepository>();
                    
                    VersionFile versionFile = new VersionFile();
                    versionFile.Id = Guid.NewGuid();
                    versionFile.FullName = fileFullName;
                    versionFile.LatestChangeset = changeset;
                    versionFile.LatestChangesetDate = DateTime.Now;
                    versionFile.LatestChangesetUser = userName;
                    versionFile.Name = fileFullName;
                    versionFile.Extention = "";
                    versionFile.LatestChangesetId = versionFileChangeset.Id;

                    //是否存在该记录
                    if (versionFileRepository.Exists(content, fileFullName))
                    {
                        if (versionFileRepository.UpdateVersionFileById(content, versionFile) && versionFileChangesetRepository.UpdateVersionFileChangesetById(content, versionFileChangeset) && versionFileDataRepository.UpdateVersionFileDataById(content, versionFileData))
                        {
                            versionResult.IsSuccess = true;
                            versionResult.Message = "签入版本成功";
                            //如果IsSuccess为true，则ResultCode为0，如果IsSuccess为false，则ResultCode为1
                            versionResult.ResultCode = 0;
                            versionResult.Changeset = changeset;
                            versionResult.FileFullName = versionFile.FullName;
                        }
                    }
                    else
                    {
                        if (versionFileRepository.AddVersionFile(content, versionFile) && versionFileChangesetRepository.AddVersionFileChangeset(content, versionFileChangeset) && versionFileDataRepository.AddVersionFileData(content, versionFileData))
                        {
                            versionResult.IsSuccess = true;
                            versionResult.Message = "签入版本成功";
                            //如果IsSuccess为true，则ResultCode为0，如果IsSuccess为false，则ResultCode为1
                            versionResult.ResultCode = 0;
                            versionResult.Changeset = changeset;
                            versionResult.FileFullName = versionFile.FullName;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                versionResult.IsSuccess = false;
                versionResult.Message = "签入版本失败";
                versionResult.ResultCode = 0;
                versionResult.Changeset = 0;
                versionResult.FileFullName = "";
            }
            return versionResult;
        }


    }
}
